package com.xyht.sca_s.student_manage_system.modules.agreement.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.agreement.entity.vo.*;
import com.xyht.sca_s.student_manage_system.modules.agreement.service.SmsAgreementApplicationService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-06-11
 */
@SecretBody
@RestController
@RequestMapping("/agreement")
public class SmsAgreementApplicationController {
    @Resource
    SmsAgreementApplicationService smsAgreementApplicationService;
    @Resource
    JwtTokenUtil jwtTokenUtil;
    @ApiModelProperty("提交协议申请")
    @PostMapping("/add")
    public ResponseResult addAgreement(@RequestBody SmsAgreementApplicationVO applicationVO,
                                       HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAgreementApplicationService.addAgreement(applicationVO,user_id);
    }

    @ApiModelProperty("修改申请表")
    @PostMapping("/update")
    public ResponseResult updateAgreement(@RequestBody SmsAgreementApplicationVO applicationVO,
                                       HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAgreementApplicationService.updateAgreement(applicationVO,user_id);
    }

    @ApiModelProperty("撤销申请")
    @PostMapping("/cancel")
    public ResponseResult cancelAgreement(@RequestBody SmsAgreementApplicationCancelVO cancelVO,
                                          HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAgreementApplicationService.cancelAgreement(cancelVO,user_id);
    }
    @ApiModelProperty("审批")
    @PostMapping("/allow")
    public ResponseResult allowAgreement(@RequestBody SmsAgreementApplicationAllowVO allowVO,
                                         HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAgreementApplicationService.allowAgreement(allowVO,user_id);
    }

    @ApiModelProperty("审批列表")
    @GetMapping("/list")
    public ResponseResult getAgreementList(
                                           @RequestParam(value = "search_str",required = false) String search_str,
                                           @RequestParam(value = "allowStatus",required = false) Integer allowStatus,
                                           @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                           @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "apply_type") Integer apply_type,
                                           @RequestParam(value = "sort",required = false)  Integer sort,
                                           HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAgreementApplicationService.getAgreementList(search_str,sort,allowStatus,pageSize,pageNum,apply_type,user_id);
    }

    @ApiModelProperty("审批详情")
    @GetMapping("/detail")
    public ResponseResult getAgreementDetail(@RequestParam(value = "apply_id") String apply_id,
                                              HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAgreementApplicationService.getAgreementDetail(apply_id,user_id);
    }
    @ApiModelProperty("申请人指定流程（申请前获取）")
    @GetMapping("/processList")
    public ResponseResult getAgreementApplicationOriginSettingProcess() {
        return smsAgreementApplicationService.getAgreementApplicationOriginSettingProcess();
    }
    @ApiModelProperty(value = "查询所有申请记录")
    @GetMapping("/selectAll")
    public ResponseResult selectAllAgreementApply(
            @RequestParam(value = "apply_unit",required = false) String apply_unit,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "status",required = false) Integer status
    ){
        return smsAgreementApplicationService.selectAllAgreementApply(apply_unit,
                pageSize,pageNum,status);
    }

    @ApiModelProperty(value = "文件下载")
    @GetMapping("/download")

    public ResponseResult tableDownload(@RequestParam(value = "apply_id") String apply_id,
                                        HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsAgreementApplicationService.tableDownload(apply_id,response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;

    }

    @ApiOperation("批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportAgreement(@RequestBody SmsAgreementExportVO smsAgreementExportVO,
                                          HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsAgreementApplicationService.exportAgreement(smsAgreementExportVO, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "强制归档")
    @PostMapping("/forced")
    public ResponseResult forcedFinishAgreementApplication(@RequestBody SmsAgreementApplicationForcedVO forcedVO,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAgreementApplicationService.forcedFinishAgreementApplication(forcedVO.getApplyId(),userId);
    }

    @ApiModelProperty(value = "强制流转")
    @PostMapping("/forcedTorsion")
    public ResponseResult forcedTorsionAgreementApplication(@RequestBody SmsAgreementApplicationDetailTorsionVO torsionVO){
        return smsAgreementApplicationService.forcedTorsionAgreementApplication(torsionVO);
    }

    @ApiModelProperty(value = "代审批")
    @PostMapping("/replaceApply")
    public ResponseResult replaceApplyAgreementApplication(HttpServletRequest request,
                                                           @RequestBody SmsAgreementApplicationAllowVO allowVO){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAgreementApplicationService.replaceApplyAgreementApplication(userId,allowVO);
    }

    @ApiModelProperty(value = "强制驳回")
    @PostMapping("/forcedReject")
    public ResponseResult forcedCancelAgreementApplication(@RequestBody SmsAgreementApplicationForcedVO forcedVO,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAgreementApplicationService.forcedRejectAgreementApplication(forcedVO,userId);
    }

    @ApiModelProperty(value = "删除协议会审单")
    @PostMapping("/deleted")
    public ResponseResult deletedAgreementApplication(@RequestParam("applyId") String applyId){
        return smsAgreementApplicationService.deletedAgreementApplication(applyId);
    }

    @ApiModelProperty(value = "修改审批意见")
    @PostMapping("/updateOpinion")
    public ResponseResult updateOpinion(@RequestBody SmsAgreementApplicationUpdateOpinionVo updateOpinionVo,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAgreementApplicationService.updateOpinion(updateOpinionVo,userId);
    }

    @ApiModelProperty(value = "修改附件")
    @PostMapping("/updateAnnex")
    public ResponseResult updateAnnex(@RequestBody SmsAgreementApplicationUpdateAnnexVo updateAnnexVo,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAgreementApplicationService.updateAnnex(updateAnnexVo,userId);
    }
}

